package entities;

import utility.vec2;
import EnemyBehaviors.BaseEnemyBehavior;
import collisionPrimitives.CRect;

import com.liquidcode.cellular.CAssetManager;
import com.liquidcode.cellular.CHealthBar;
import com.liquidcode.cellular.R;
import com.liquidcode.cellular.Texture;

public class CEnemy extends CBaseObject 
{
	protected int m_nAttackDamage; 
	protected int m_nHealth; 
	protected CHealthBar m_healthBar;
	protected CSprite m_sprite;
	protected float m_healthOffset;
	protected CRect m_rect;
	protected float m_fSpeed = 0.0f;
	protected BaseEnemyBehavior m_currentBehavior = null;
	protected float FORCE_VALUE = 0.05f;
	protected float ENEMY_MOVE_SPEED = 1.0f;
	
	public CEnemy()
	{
		TAG = "enemy";
		m_nHealth = 100;
		m_nAttackDamage = 4;
		m_sprite = new CSprite();
		m_healthBar = new CHealthBar(this);
		m_rect = new CRect();
		m_fSpeed = ENEMY_MOVE_SPEED;
	}
	
	@Override
	public void Update(float dt)
	{
		if( alive == false )
		{
			return;
		}
		// update the behavior 
		if( m_currentBehavior != null )
			m_currentBehavior.RunBehavior(dt);
		
		m_healthBar.setPosX(this.pos.getX());
		m_healthBar.setPosY(this.pos.getY() - m_healthOffset);
		m_healthBar.Update(dt);
		//alive = m_healthBar.getAlive();
		
		m_rect.min.setX(pos.getX() - (m_sprite.getWidth()*scale)*0.5f);
		m_rect.min.setY(pos.getY() - (m_sprite.getHeight()*scale)*0.5f);
		m_rect.max.setX(pos.getX() + (m_sprite.getWidth()*scale)*0.5f);
		m_rect.max.setY(pos.getY() + (m_sprite.getHeight()*scale)*0.5f);
	}
	
	public void InitHealthBar()
	{
		Texture t = CAssetManager.GetInstance().getTexture(R.drawable.health_green);
		m_healthBar.getFrontSprite().setTexture(t, false);
		
		t = CAssetManager.GetInstance().getTexture(R.drawable.health_red);
		m_healthBar.getBackSprite().setTexture(t, true);		
		m_healthBar.setScaleFactor(0.5f);
	}
	
	public boolean ChangeBehavior(BaseEnemyBehavior b)
	{
		if( b == null ) 
			return false; 
		
		m_currentBehavior = b; 
		m_currentBehavior.Init(this);
		
		return true;
	}
	
	public void PushBack(vec2 force)
	{
		float forceX = force.getX() * FORCE_VALUE;
		float forceY = force.getY() * FORCE_VALUE;
		pos.setX(pos.getX() + forceX);
		pos.setY(pos.getY() + forceY);
	}
	
	// Acessors 
	public int getAttackDamage() { return m_nAttackDamage; }
	public int getHealth() { return m_nHealth; }
	public CSprite getSprite() { return m_sprite; }
	public CHealthBar getHealthBar() { return m_healthBar; }
	public float getHealthOffset() { return m_healthOffset; }
	public CRect getCollisionRect() { return m_rect; }
	public float getSpeed() { return m_fSpeed; }
	
	// Mutators
	public void setAttackDamage(int d) { m_nAttackDamage = d; }
	public void setHealth(int h) { m_nHealth = h; }
	public void setHealthOffset(float amount) { m_healthOffset = amount; }
	public void setSpeed(float s) { m_fSpeed = s; }
}
